<template>
  <div class="app-container">
    <Find @handleBlur1="handleBlur1" @Launchform="Launchform3" :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findData" />

    <el-card style="margin-top: 10px" shadow="always" :body-style="{ padding: '5px' }">
      <div style="display: flex">
        <div style="width: 23%">
          <h1>取消/检核月份</h1>
          <Find
            @Launchform="Launchform"
            :OriginalFilter="{ static: true }"
            :searchImg="false"
            style="margin-bottom: 10px"
            :formList="formList1"
            @findClicked="findData1"
          />
        </div>
        <div style="margin-left: 15px; margin-top: 5px">
          <h1>数据取数/重算作业</h1>
          <Find :OriginalFilter="{ static: true }" :searchImg="false" style="margin-bottom: 10px" :formList="formList2" @findClicked="findData2" />
        </div>
      </div>

      <div>
        <div>
          <h1>资料明细</h1>
          <h1 style="color: red">双击查询调岗明细</h1>
          <Table
            :summaryData="summaryData"
            summary
            @inputChange="inputChange"
            @selectList="selectList"
            @currentRow1="currentRow1"
            :isLoading="isLoading3"
            :selection="true"
            :tableHeader="tableHeader"
            :tableData="DetailOfDataTableDate"
            :TableHeight="82"
          />
          <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
        </div>
      </div>

      <el-dialog v-model="dialogVisible1" width="50%" class="dialog pb0">
        <el-button type="primary" size="default" @click="dialogVisible2 = true">新增</el-button>
        <Tables
          @modifTable1="modifTable1"
          @deleteRow="deleteRow"
          :functionList1="false"
          :functionList="true"
          :updated="true"
          :deleteButton="true"
          :isLoading="isLoading3"
          :tableHeader="tableHeader1"
          :tableData="FilingOfRoyaltyRatesTableDate"
          :TableHeight="78"
        />
      </el-dialog>

      <el-dialog v-model="dialogVisible2" width="50%" class="dialog pb0">
        <Find
          @Launchform="Launchform5"
          :OriginalFilter="{ static: true }"
          :searchImg="false"
          style="margin-bottom: 10px"
          :formList="formList3"
          @findClicked="findData5"
        />
      </el-dialog>

      <el-dialog v-model="dialogVisible3" width="50%" class="dialog pb0">
        <el-button type="primary" size="default" @click="dialogVisible5 = true">新增</el-button>
        <Tables
          @modifTable1="modifTable"
          @deleteRow="deleteRow1"
          :functionList1="false"
          :functionList="true"
          :updated="true"
          :deleteButton="true"
          :isLoading="isLoading3"
          :tableHeader="tableHeader2"
          :tableData="ManagementGroupTableDate"
          :TableHeight="78"
        />
      </el-dialog>

      <el-dialog v-model="dialogVisible5" width="50%" class="dialog pb0">
        <Find
          @Launchform="Launchform6"
          :OriginalFilter="{ static: true }"
          :searchImg="false"
          style="margin-bottom: 10px"
          :formList="formList5"
          @findClicked="findData6"
        />
      </el-dialog>

      <el-dialog v-model="dialogVisible7" width="50%" class="dialog pb0">
        <Table :isLoading="isLoading3" :selection="true" :tableHeader="tableHeader3" :tableData="AssignmentDetailsTableDate" :TableHeight="78" />
      </el-dialog>

      <!-- 部门 -->
      <el-dialog v-model="dialogVisible8" width="20%" class="dialog pb0">
        <el-button type="primary" size="default" @click="cofimFlag">确认</el-button>

        <Table @selectList="selectList1" :selection="true" :tableHeader="tableHeader5" :tableData="deptNameListParms" :TableHeight="78" />
      </el-dialog>
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import { JobInquiry, JobQuery } from '@/api/GeneralFinanceDepartment/GeneralFinanceDepartment/FactoryLaborWage'
import fileRequest from '@/utils/fileRequest'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'PersonnelPayrollSettlement'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import type { formListType } from '@/components/Find/index.vue'
import { ref, onMounted } from 'vue'
import {
  RecalculationOf,
  RecalculationOfWages,
  GetPerformance,
  salaryCalculator,
  AssignmentDetails,
  cancelDate,
  PickupPay,
  housePay,
  DetailOfData,
  deptNameQuery,
  fetchQuery,
  TakeMonthlyAttendance,
  checkCancelCheck,
  FilingOfRoyaltyRates,
  FilingDelter,
  FilingDelter1,
  addFiling,
  checkMonthSalary,
  ManagementGroup,
  ManagementDelter,
  ManagementDelter1,
  addManagement
} from '@/api/GeneralFinanceDepartment/FactoryLaborWage/PayrollClosingOperation'
import { ElMessage, ElMessageBox, ElProgress } from 'element-plus'
import LoginWork from '@/utils/LoginWork'

const summaryData = ref<any>([
  {
    index: 15,
    num: ''
  },
  {
    index: 14,
    num: 0
  },
  {
    index: 15,
    num: 0
  },
  {
    index: 16,
    num: 0
  },
  {
    index: 17,
    num: 0
  },
  {
    index: 18,
    num: 0
  },
  {
    index: 19,
    num: 0
  },
  {
    index: 20,
    num: 0
  },
  {
    index: 21,
    num: 0
  },
  {
    index: 22,
    num: 0
  },
  {
    index: 23,
    num: 0
  },
  {
    index: 24,
    num: 0
  },
  {
    index: 25,
    num: 0
  },
  {
    index: 26,
    num: 0
  },
  {
    index: 27,
    num: 0
  },

  {
    index: 28,
    num: 0
  },
  {
    index: 29,
    num: 0
  },
  {
    index: 30,
    num: 0
  },
  {
    index: 31,
    num: 0
  },
    {
    index: 32,
    num: 0
  },
  {
    index: 33,
    num: 0
  },
  {
    index: 34,
    num: 0
  },
  {
    index: 35,
    num: 0
  }
])

// 回车方法
const dialogVisible8 = ref(false)

const handleBlur1 = (row: any) => {
  if (row[0].value !== '') {
    DepartmentInquiryMethods()
    dialogVisible8.value = true
  }
}

const inputChange = async (row: any) => {
  const { workerId, companyId, yyyymm, idNo, deptName, remark } = row
  const result = await LoginWork.post<any, any>(`/personnel/UpdateRemark`, { workerId, companyId, yyyymm, idNo, deptName, remark })
  if (result.code === 200) {
    DetailOfDataMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 重算工资
const RecalculationOfWagesParms = ref([])

const selectList = (row: any) => {
  RecalculationOfWagesParms.value = row.map((item: any) => {
    const { workerId, companyId, yyyymm, idNo, deptName } = item
    return { workerId, companyId, yyyymm, idNo, deptName }
  })
}

const RecalculationOfWagesMethods = async () => {
  try {
    const result = await RecalculationOfWages(RecalculationOfWagesParms.value)
    if (result.code === 200) {
      DetailOfDataMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('重算工资失败失败请重试')
  }
}

// 取绩效工资
const GetPerformanceMethods = async () => {
  try {
    const result = await GetPerformance(fetchQueryParms.value)
    if (result.code === 200) {
      DetailOfDataParms.value.yyyymm = fetchQueryParms.value.years
      formList.value[0].dateValue = fetchQueryParms.value.years
      DetailOfDataMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('取绩效工资失败请重试')
  }
}

// 计算工资
const salaryCalculatorMethods = async () => {
  try {
    const result = await salaryCalculator(fetchQueryParms.value)
    if (result.code === 200) {
      DetailOfDataParms.value.yyyymm = fetchQueryParms.value.years
      formList.value[0].dateValue = fetchQueryParms.value.years
      DetailOfDataMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('计算工资失败请重试')
  }
}

// 调岗明细查询
const dialogVisible7 = ref(false)

const AssignmentDetailsParms = ref({
  yyyymm: '',
  workerId: ''
})

const AssignmentDetailsTableDate = ref([])

const AssignmentDetailsMethods = async () => {
  try {
    const result = await AssignmentDetails(AssignmentDetailsParms.value.yyyymm, AssignmentDetailsParms.value.workerId)
    AssignmentDetailsTableDate.value = result
  } catch (error) {
    JMessage.warning('调岗明细查询出错')
  }
}

const currentRow1 = async (row: any) => {
  dialogVisible7.value = true
  AssignmentDetailsParms.value.workerId = row.workerId
  AssignmentDetailsParms.value.yyyymm = row.yyyymm
  AssignmentDetailsMethods()
}

// 清空資料
const cancelDateMethods = async () => {
  try {
    const result = await cancelDate(fetchQueryParms.value)
    if (result.code === 200) {
      DetailOfDataParms.value.yyyymm = fetchQueryParms.value.years
      formList.value[0].dateValue = fetchQueryParms.value.years
      DetailOfDataMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('清空资料失败请重试')
  }
}

// 幾件
const PickupPayMethods = async () => {
  try {
    const result = await PickupPay(fetchQueryParms.value)
    if (result.code === 200) {
      DetailOfDataParms.value.yyyymm = fetchQueryParms.value.years
      formList.value[0].dateValue = fetchQueryParms.value.years
      DetailOfDataMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('计件失败请重试')
  }
}

// 计时工资
const housePayMethods = async () => {
  try {
    const result = await housePay(fetchQueryParms.value)
    if (result.code === 200) {
      DetailOfDataParms.value.yyyymm = fetchQueryParms.value.years
      formList.value[0].dateValue = fetchQueryParms.value.years
      DetailOfDataMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('计时工资失败请重试')
  }
}

// 新增
const dialogVisible5 = ref(false)

const addManagementParms = ref({
  deptName: '',
  deptName2: '',
  deptNo: '',
  deptNo2: '',
  rate: '',
  remark: ''
})

const Launchform6 = (row: any) => {
  const deptNa = row[0].selectValue.split(' ')
  const deptNa1 = row[2].selectValue.split(' ')
  addManagementParms.value.deptName = deptNa[0]
  addManagementParms.value.deptNo = deptNa[3]
  formList5.value[1].inputValue = deptNa[3]
  addManagementParms.value.deptName2 = deptNa1[0]
  addManagementParms.value.deptNo2 = deptNa1[3]
  addManagementParms.value.rate = row[3].inputValue
  addManagementParms.value.remark = row[4].inputValue
}

const findData6 = (row: any) => {
  if (row === '确认') {
    if (addManagementParms.value.deptName === '' || addManagementParms.value.deptName2 === '' || addManagementParms.value.rate === '') {
      JMessage.warning('数据不完善')
    } else {
      aaddManagementMethods()
    }
  }

  if (row === '置空') {
    formList5.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
    })
  }
}

const aaddManagementMethods = async () => {
  try {
    const result = await addManagement(addManagementParms.value)
    if (result.code === 200) {
      ManagementGroupMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('新增出错请重试')
  }
}

// 修改
const modifTable = async (row: any) => {
  const deptNa = row.deptName.selectValue.split(' ')
  const deptNa1 = row.deptName2.selectValue.split(' ')
  row.deptName = deptNa[0]
  row.deptNo = deptNa[3]
  row.deptName2 = deptNa1[0]
  row.deptNo2 = deptNa1[3]
  try {
    const result = await ManagementDelter1(row)
    if (result.code === 200) {
      ManagementGroupMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('删除失败')
  }
}

const MethodsParms = ref({
  id: ''
})

const deleteRow1 = (row: any) => {
  MethodsParms.value.id = row.row.id
  handleCloseMateriaLKlc()
}

const handleCloseMateriaLKlc = () => {
  if (MethodsParms.value.id === '') {
    JMessage.warning('请选择需要删除的数据')
  } else {
    ElMessageBox.confirm('确认要删除数据吗?')
      .then(async () => {
        ManagementDelterMethods()
      })
      .catch(() => {})
  }
}

const ManagementDelterMethods = async () => {
  try {
    const result = await ManagementDelter(MethodsParms.value.id)
    if (result.code === 200) {
      ManagementGroupMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('删除失败')
  }
}

// 管理组别建档
const dialogVisible3 = ref(false)

const ManagementGroupTableDate = ref([])

const ManagementGroupMethods = async () => {
  try {
    const result = await ManagementGroup()
    ManagementGroupTableDate.value = result.map((item: any) => {
      item.deptName = {
        selectValue: item.deptName || '',
        options: DepartmentInquiryMethodsTableDate.value
      }

      item.deptName2 = {
        selectValue: item.deptName2 || '',
        options: DepartmentInquiryMethodsTableDate.value
      }
      return item
    })
  } catch (error) {
    JMessage.error('管理组别建档建档查询失败')
  }
}

// 月工资检核取消检核
const formatCurrentDate = (date?: any) => {
  let year = date.getFullYear()
  let month = date.getMonth() + 1 // 月份是从 0 开始计算的

  // 如果当前月份是 1 月,则取去年的 12 月
  if (month === 1) {
    month = 12
    year--
  }

  // 将月份格式化为两位数
  month = month.toString().padStart(2, '0')

  return `${year}${month}`
}

const currentDate = ref(new Date())

const fetchQueryParms = ref({
  years: ''
})

const checkMonthSalaryParms = ref()

const checkMonthSalaryMethods = async () => {
  try {
    const result = await checkMonthSalary(checkMonthSalaryParms.value, fetchQueryParms.value)
    if (result.code === 200) {
      DetailOfDataParms.value.yyyymm = fetchQueryParms.value.years
      formList.value[0].dateValue = fetchQueryParms.value.years
      DetailOfDataMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('检核出错请重试')
  }
}

// 新增
const dialogVisible2 = ref(false)

const formList3 = ref<any[]>([
  {
    type: 'input',
    inputPlaceholder: '部门',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '产值起',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '产值止',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '提成比率',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '备注',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '确认'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])

const formList5 = ref<any[]>([
  {
    type: 'select',
    selectValue: '',
    selectP: '管理组别',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '管理分组代码',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '管理分组名称',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '提成比率',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '备注',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '确认'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])

const findData5 = (row: any) => {
  if (row === '确认') {
    if (
      addFilingParms.value.deptName === '' ||
      addFilingParms.value.amt1 === '' ||
      addFilingParms.value.amt2 === '' ||
      addFilingParms.value.rate === ''
    ) {
      JMessage.warning('数据不完善')
    } else {
      addFilingMethods()
    }
  }

  if (row === '置空') {
    formList3.value.forEach((item: any) => {
      item.inputValue = ''
    })
  }
}

const Launchform5 = (row: any) => {
  addFilingParms.value.deptName = row[0].inputValue
  addFilingParms.value.amt1 = row[1].inputValue
  addFilingParms.value.amt2 = row[2].inputValue
  addFilingParms.value.rate = row[3].inputValue
  addFilingParms.value.remark = row[4].inputValue
}

// 提成比率新增
const addFilingParms = ref({
  amt1: '',
  amt2: '',
  deptName: '',
  rate: '',
  remark: ''
})

const addFilingMethods = async () => {
  try {
    const result = await addFiling(addFilingParms.value)
    if (result.code === 200) {
      FilingOfRoyaltyRatesMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('新增出错请重试')
  }
}

// 管理组别修改
const modifTable1 = async (row: any) => {
  try {
    const result = await FilingDelter1(row)
    if (result.code === 200) {
      FilingOfRoyaltyRatesMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('删除失败')
  }
}

// 删除
const deleteUserMethodsParms = ref({
  id: ''
})

const deleteRow = (row: any) => {
  deleteUserMethodsParms.value.id = row.row.id
  handleCloseMateriaLKlc5()
}

const handleCloseMateriaLKlc5 = () => {
  if (deleteUserMethodsParms.value.id === '') {
    JMessage.warning('请选择需要删除的数据')
  } else {
    ElMessageBox.confirm('确认要删除数据吗?')
      .then(async () => {
        deleteUserMethods()
      })
      .catch(() => {})
  }
}

const deleteUserMethods = async () => {
  try {
    const result = await FilingDelter(deleteUserMethodsParms.value.id)
    if (result.code === 200) {
      FilingOfRoyaltyRatesMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('删除失败')
  }
}

// 提成比率建档
const dialogVisible1 = ref(false)

const FilingOfRoyaltyRatesTableDate = ref([])

const FilingOfRoyaltyRatesMethods = async () => {
  try {
    const result = await FilingOfRoyaltyRates()
    FilingOfRoyaltyRatesTableDate.value = result.map((item: any) => {
      item.change = 'Y'
      item.disableConfirm = true
      return item
    })
  } catch (error) {
    JMessage.error('提成比率建档查询失败')
  }
}

// 检核取消检核
const findData2 = (row: string) => {
  if (row === '1、取月考勤资料') {
    if (fetchQueryParms.value.years === '') {
      JMessage.warning('月份不可为空!!!')
    } else {
      TakeMonthlyAttendanceMethods()
    }
  }

  if (row === '检核月考勤资料') {
    checkCancelCheckParms.value = 1
    if (fetchQueryParms.value.years === '') {
      JMessage.warning('月份不可为空!!!')
    } else {
      checkCancelCheckMethods()
    }
  }

  if (row === '取消检核月考勤') {
    checkCancelCheckParms.value = 2
    if (fetchQueryParms.value.years === '') {
      JMessage.warning('月份不可为空!!!')
    } else {
      checkCancelCheckMethods()
    }
  }

  if (row === '提成比率建档') {
    FilingOfRoyaltyRatesMethods()
    dialogVisible1.value = true
  }

  if (row === '检核月工资') {
    checkMonthSalaryParms.value = 1
    if (fetchQueryParms.value.years === '') {
      JMessage.warning('月份不可为空!!!')
    } else {
      checkMonthSalaryMethods()
    }
  }

  if (row === '取消检核月工资') {
    checkMonthSalaryParms.value = 2
    if (fetchQueryParms.value.years === '') {
      JMessage.warning('月份不可为空!!!')
    } else {
      checkMonthSalaryMethods()
    }
  }

  if (row === '管理组别建档') {
    dialogVisible3.value = true
    ManagementGroupMethods()
  }

  if (row === '2、取计时工资') {
    housePayMethods()
  }
  if (row === '3、取计件工资') {
    PickupPayMethods()
  }

  if (row === '5、计算工资') {
    salaryCalculatorMethods()
  }

  if (row === '4、取绩效工资') {
    GetPerformanceMethods()
  }

  if (row === '重算工资') {
    if (RecalculationOfWagesParms.value.length === 0) {
      JMessage.warning('请勾选数据')
    } else {
      RecalculationOfWagesMethods()
    }
  }
}

const checkCancelCheckParms = ref()

const checkCancelCheckMethods = async () => {
  try {
    const result = await checkCancelCheck(checkCancelCheckParms.value, fetchQueryParms.value)
    if (result.code === 200) {
      DetailOfDataParms.value.yyyymm = fetchQueryParms.value.years
      formList.value[0].dateValue = fetchQueryParms.value.years
      DetailOfDataMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('检核出错请重试')
  }
}

// 取月考勤资料
const TakeMonthlyAttendanceMethods = async () => {
  try {
    const result = await TakeMonthlyAttendance(fetchQueryParms.value)
    if (result.code === 200) {
      DetailOfDataParms.value.yyyymm = fetchQueryParms.value.years
      formList.value[0].dateValue = fetchQueryParms.value.years
      DetailOfDataMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('取数失败请重试')
  }
}

// 取数

const Launchform = (row: any) => {
  fetchQueryParms.value.years = row[0].dateValue
}

const findData1 = (row: string) => {
  if (row === '取数') {
    handleCloseMateriaLKlc12()
  }
  if (row === '清空资料') {
    handleCloseMateriaLKlc1()
  }
}

const handleCloseMateriaLKlc12 = () => {

ElMessageBox.confirm('确认要取数吗?')
  .then(async () => {
    fetchQueryMethods()
  })
  .catch(() => {})

}

const handleCloseMateriaLKlc1 = () => {

    ElMessageBox.confirm('确认要清空数据吗?')
      .then(async () => {
        cancelDateMethods()
      })
      .catch(() => {})
  
}


const fetchQueryMethods = async () => {
  try {
    const result = await fetchQuery(fetchQueryParms.value)
    if (result.code === 200) {
      DetailOfDataParms.value.yyyymm = fetchQueryParms.value.years
      formList.value[0].dateValue = fetchQueryParms.value.years
      DetailOfDataMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('取数失败请重试')
  }
}

// 工种查询
const JobQueryParms = ref({
  pageNum: 1,
  pageSize: 300,
  workType: ''
})

const JobQueryMethods = async () => {
  try {
    const result = await JobQuery(JobQueryParms.value)

    let NewArray = result.list.map((item: any) => {
      return item.workTypeShort
    })

    formList.value[6].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
  } catch (error) {
    JMessage.error('工种查询出错')
  }
}

// 职务查询
const JobInquiryMethods = async () => {
  try {
    let NewArray = <any>[]

    const result = await JobInquiry()

    NewArray = result.reduce((arr: any, item: any) => {
      if (item.positionSeq && item.position) {
        // 只添加非空的 typeName 和 typeNo
        arr.push(item.positionSeq + ' ' + item.position)
      }
      return arr
    }, [])

    formList.value[5].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
  } catch (error) {
    JMessage.error('工种查询出错')
  }
}

// 部门查询
const EvaluatesTheArrayReturnValue = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      optionKey: index,
      optionValue: item,
      optionLabel: item
    }
  })
}

const DepartmentInquiryMethodsTableDate = ref([])

const deptNameListParms = ref([])

const deptNa1Name = ref('')

const DepartmentInquiryMethods = async () => {
  let NewArray = []
  const result = await deptNameQuery(deptNa1Name.value)
  NewArray = result.reduce((arr: any, item: any) => {
    if (item.deptName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.deptName)
    }
    return arr
  }, [])
  formList.value[1].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
  deptNameListParms.value = NewArray.map((item: any) => {
    return { deptName: item }
  })

  NewArray = result.reduce((arr: any, item: any) => {
    if (item.deptName && item.deptNo) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.deptName + '   ' + item.deptNo)
    }
    return arr
  }, [])
  DepartmentInquiryMethodsTableDate.value = EvaluatesTheArrayReturnValue1(NewArray)
  formList5.value[0].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
  formList5.value[2].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
}

const EvaluatesTheArrayReturnValue1 = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      value: item,
      label: item
    }
  })
}

// 资料明细查询

const selectList1 = (row: any) => {
  DetailOfDataParms.value.deptNameList = row.map((item: any) => {
    return item.deptName
  })
  detpNameLists.value = DetailOfDataParms.value.deptNameList
}

const DetailOfDataParms = ref<any>({
  bankCardId: '',
  bankCardName: '',
  bankCardUser: '',
  calcType: '',
  companyId: '',
  deptNameList: [],
  flag: '',
  idNo: '',
  inTime: '',
  name: '',
  outTime: '',
  pageNum: 1,
  pageSize: 40,
  position: '',
  sex: '',
  workType: '',
  workerId: '',
  yyyymm: ''
})
const detpNameLists = ref([])
const cofimFlag = () => {
  dialogVisible8.value = false
  DetailOfDataMethods()
  if (detpNameLists.value[1] !== undefined) {
    formList.value[1].inputValue = detpNameLists.value[0] + ' ' + detpNameLists.value[1]
  } else {
    formList.value[1].inputValue = detpNameLists.value[0]
  }
}

const Launchform3 = (row: any) => {
  DetailOfDataParms.value.yyyymm = row[0].dateValue
  // DetailOfDataParms.value.deptNameList = row[1].selectValue1
  deptNa1Name.value = row[1].inputValue
  DetailOfDataParms.value.workerId = row[2].inputValue
  DetailOfDataParms.value.name = row[3].inputValue
  DetailOfDataParms.value.sex = row[4].selectValue
  DetailOfDataParms.value.position = row[5].selectValue.split(' ')[1]
  DetailOfDataParms.value.workType = row[6].selectValue
  DetailOfDataParms.value.calcType = row[7].selectValue
  DetailOfDataParms.value.inTime = row[8].dateValue[0]
  DetailOfDataParms.value.outTime = row[8].dateValue[1]
  DetailOfDataParms.value.flag = row[9].checked === true ? 1 : ''
  DetailOfDataParms.value.bankCardUser = row[10].inputValue
  DetailOfDataParms.value.bankCardName = row[11].inputValue
  DetailOfDataParms.value.bankCardId = row[12].inputValue
  DetailOfDataParms.value.idNo = row[13].inputValue
  if (row[1].inputValue === '') {
    DetailOfDataParms.value.deptNameList = []
  }
}

const isLoading3 = ref(false)

const DetailOfDataTableDate = ref([])

const DetailOfDataMethods = async () => {
  isLoading3.value = true

  const result = await DetailOfData(DetailOfDataParms.value)
  if (result.pcSalaryNewList.length > 0) {
    DetailOfDataTableDate.value = result.pcSalaryNewList.map((item: any) => {
      if (item.cfmFlag === 'Y') {
        item.backgroundColor = '#3FFF38'
      }
      if (item.salaryFlag === 'Y') {
        item.backgroundColor3 = '#3FFF38'
      }
      if (item.flag === 'Y') {
        item.backgroundColor2 = '#FF00FF'
      }

      item.change = 'Y'
      item.disableConfirm = true
      return item
    })
  } else {
    DetailOfDataTableDate.value = []
  }


  if (result && result.count) {
    summaryData.value[1].num = result.count.basicSalary
    summaryData.value[2].num = result.count.positionAlw
    summaryData.value[3].num = result.count.positionAlw2
    summaryData.value[4].num = result.count.salaryAlw
    summaryData.value[5].num = result.count.minSalary
    summaryData.value[6].num = result.count.overSalary
    summaryData.value[7].num = result.count.allowance
    summaryData.value[8].num = result.count.injuryAlw
    summaryData.value[9].num = result.count.introduceBonus
    summaryData.value[10].num = result.count.leaveAlw
    summaryData.value[11].num = result.count.unionM
    summaryData.value[12].num = result.count.taxM
    summaryData.value[13].num = result.count.clothesM
    summaryData.value[14].num = result.count.waterEleM
    summaryData.value[15].num = result.count.mealM
    summaryData.value[16].num = result.count.absenceM
    summaryData.value[17].num = result.count.waterM
    summaryData.value[18].num = result.count.repayM
    summaryData.value[19].num = result.count.carFee
    summaryData.value[20].num = result.count.otherM
    summaryData.value[21].num = result.count.minusM
    summaryData.value[22].num = result.count.salaryAct
  }

  total.value = result.total
  isLoading3.value = false
}

const total = ref(0)

const handleSizeChange = (val: number) => {
  DetailOfDataParms.value.pageSize = val
  DetailOfDataMethods()
}

const handleCurrentChange = (val: number) => {
  DetailOfDataParms.value.pageNum = val
  DetailOfDataMethods()
}

// 月份查询
const RecalculationOfMehtods = async () => {
  const result = await RecalculationOf()
  if (result.code === 200) {
    formList.value[0].dateValue = result.data
    formList1.value[0].dateValue = result.data
    fetchQueryParms.value.years = result.data
    DetailOfDataParms.value.yyyymm = result.data
  } else {
    formList.value[0].dateValue = formatCurrentDate(currentDate.value)
    formList1.value[0].dateValue = formatCurrentDate(currentDate.value)
    fetchQueryParms.value.years = formatCurrentDate(currentDate.value)
    DetailOfDataParms.value.yyyymm = formatCurrentDate(currentDate.value)
  }
  DetailOfDataMethods()
}

onMounted(() => {
  RecalculationOfMehtods()

  DepartmentInquiryMethods()

  JobInquiryMethods()

  JobQueryMethods()
})

// 日期
const formList = ref<any[]>([
  {
    type: 'dateMonth',
    dateLabel: '月份',
    dateValue: '',
    dateType: 'month'
  },
  {
    type: 'input',
    inputPlaceholder: '部门',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '工号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '姓名',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '性别',
    selectLabel: '',
    selectOptions: [
      { optionKey: 1, optionValue: '男', optionLabel: '男' },
      { optionKey: 2, optionValue: '女', optionLabel: '女' }
    ]
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '职务',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '工种',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '计件/计时',
    selectLabel: '',
    selectOptions: [
      {
        optionKey: 1,
        optionValue: '计时',
        optionLabel: '计时'
      },
      {
        optionKey: 2,
        optionValue: '计件',
        optionLabel: '计件'
      }
    ]
  },
  {
    type: 'date',
    dateLabel: '进厂日期',
    dateValue: ''
  },
  {
    type: 'checkbox',
    checkboxLabel: '实发<0',
    checked: false
  },

  // {
  //   type: 'input',
  //   inputPlaceholder: '开户名',
  //   inputValue: ''
  // },
  // {
  //   type: 'input',
  //   inputPlaceholder: '开户行',
  //   inputValue: ''
  // },
  // {
  //   type: 'input',
  //   inputPlaceholder: '银行卡号',
  //   inputValue: ''
  // },
  // {
  //   type: 'input',
  //   inputPlaceholder: '身份证号',
  //   inputValue: ''
  // },
  {
    type: 'button',
    checkboxLabel: '搜索',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出工资核算表'
  },
  {
    type: 'button',
    checkboxLabel: '导出工资签核表'
  },
  {
    type: 'button',
    checkboxLabel: '导出银行确认表'
  },
  {
    type: 'button',
    checkboxLabel: '导出工资条'
  }
])

const formList1 = ref<any[]>([
  {
    type: 'dateMonth',
    dateLabel: '月份',
    dateValue: '',
    dateType: 'month'
  },
  {
    type: 'button',
    checkboxLabel: '取数'
  },
  {
    type: 'button',
    checkboxLabel: '清空资料'
  }
])

const formList2 = ref<any[]>([
  {
    type: 'button',
    checkboxLabel: '1、取月考勤资料'
  },
  {
    type: 'button',
    checkboxLabel: '检核月考勤资料'
  },
  {
    type: 'button',
    checkboxLabel: '取消检核月考勤'
  },
  {
    type: 'button',
    checkboxLabel: '2、取计时工资'
  },
  {
    type: 'button',
    checkboxLabel: '3、取计件工资'
  },
  {
    type: 'button',
    checkboxLabel: '提成比率建档'
  },
  {
    type: 'button',
    checkboxLabel: '管理组别建档'
  },
  {
    type: 'button',
    checkboxLabel: '4、取绩效工资'
  },
  {
    type: 'button',
    checkboxLabel: '5、计算工资'
  },
  {
    type: 'button',
    checkboxLabel: '重算工资'
  },
  {
    type: 'button',
    checkboxLabel: '检核月工资'
  },
  {
    type: 'button',
    checkboxLabel: '取消检核月工资'
  }
])

const findData = async (row: string) => {
  if (row === '搜索') {
    DetailOfDataMethods()
  }
  if (row === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.dateValue = ''
      item.selectValue = ''
      item.selectValue1 = []
    })
  }

  if (row === '导出工资核算表') {
    if (DetailOfDataTableDate.value.length === 0) {
      JMessage.warning('该月份暂无数据不可导出')
      return
    }
    const res = (await fileRequest.post(`/personnel/ExportSalaryAccountingTable`, DetailOfDataParms.value)) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }

  if (row === '导出工资签核表') {
    if (DetailOfDataTableDate.value.length === 0) {
      JMessage.warning('该月份暂无数据不可导出')
      return
    }
    const res = (await fileRequest.post(`/personnel/ExportSalaryApprovalForm`, DetailOfDataParms.value)) as any

    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }

  if (row === '导出银行确认表') {
    if (DetailOfDataTableDate.value.length === 0) {
      JMessage.warning('该月份暂无数据不可导出')
      return
    }
    const res = (await fileRequest.post(`/personnel/ExportSalaryBank`, DetailOfDataParms.value)) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }

  if (row === '导出工资条') {
    if (DetailOfDataTableDate.value.length === 0) {
      JMessage.warning('该月份暂无数据不可导出')
      return
    }
    const res = (await fileRequest.post(`/personnel/ExportPayslip`, DetailOfDataParms.value)) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}

// 表格数据

const tableHeader = ref([
  {
    label: '月份',
    prop: 'yyyymm',
    width: '55',
    backgroundColor3: true,
    change: true
  },
  {
    label: '部门',
    prop: 'deptName',
    width: '55',
    change: true
  },
  {
    label: '工号',
    prop: 'workerId',
    width: '55',
    change: true
  },
  {
    label: '姓名',
    prop: 'name',
    width: '55',
    change: true
  },
  {
    label: '年龄',
    prop: 'age',
    width: '55',
    change: true
  },
  {
    label: '职务',
    prop: 'position',
    width: '55',
    change: true
  },
  {
    label: '工种',
    prop: 'workType',
    width: '55',
    change: true
  },
  {
    label: '计时/计件',
    prop: 'calcType',
    change: true
  },
  {
    label: '进厂日期',
    prop: 'dateIn',
    change: true
  },
  {
    label: '离职日期',
    prop: 'delDate',
    change: true
  },
  {
    label: '离职',
    prop: 'delReason',
    change: true
  },

  {
    label: '自然天数',
    prop: 'attendance',
    change: true
  },
  {
    label: '出勤天数',
    prop: 'attendanceAct',
    backgroundColor: true,
    change: true
  },
  {
    label: '基本工资',
    prop: 'basicSalary',
    change: true
  },
  {
    label: '计时工资',
    prop: 'positionAlw',
    change: true,
    backgroundColor2: true
  },
  {
    label: '计件工资',
    prop: 'positionAlw2',
    change: true
  },
  {
    label: '绩效工资',
    prop: 'salaryAlw',
    change: true
  },
  {
    label: '补贴',
    prop: 'minSalary',
    change: true
  },
  {
    label: '加班工资',
    prop: 'overSalary',
    change: true
  },
  {
    label: '奖励',
    prop: 'allowance',
    change: true
  },
  {
    label: '补发',
    prop: 'injuryAlw',
    change: true
  },
  {
    label: '其他',
    prop: 'introduceBonus',
    change: true
  },
  {
    label: '小计',
    prop: 'leaveAlw',
    change: true
  },
  {
    label: '社医保',
    prop: 'unionM',
    change: true
  },
  {
    label: '个税',
    prop: 'taxM',
    change: true
  },
  {
    label: '厂服厂牌费用',
    prop: 'clothesM',
    change: true
  },
  {
    label: '水电费',
    prop: 'waterEleM',
    change: true
  },
  {
    label: '迟到漏打卡',
    prop: 'mealM',
    change: true
  },

  {
    label: '旷工扣款',
    prop: 'absenceM',
    change: true
  },
  {
    label: '倒欠工资',
    prop: 'waterM',
    change: true
  },
  {
    label: '个人借款',
    prop: 'repayM',
    change: true
  },
      {
    label: '停车费',
    prop: 'carFee',
    change: true
  },
  {
    label: '其他乐捐',
    prop: 'otherM',
    change: true
  },

  {
    label: '小计',
    prop: 'minusM',
    change: true
  },
  {
    label: '实发工资',
    prop: 'salaryAct',
    change: true
  },
  {
    label: '备注',
    prop: 'remark'
  },
  {
    label: '更新人员',
    prop: 'updateUser',
    change: true
  },

  {
    label: '更新时间',
    prop: 'updateDate',
    change: true
  }
])

const tableHeader1 = ref([
  {
    label: '部门',
    prop: 'deptName',
    type: 'input'
  },
  {
    label: '产值起',
    prop: 'amt1',
    type: 'input'
  },
  {
    label: '产值止',
    prop: 'amt2',
    type: 'input'
  },
  {
    label: '提成比率',
    prop: 'rate',
    type: 'input'
  },
  {
    label: '备注',
    prop: 'remark',
    type: 'input'
  }
])

const tableHeader2 = ref([
  {
    label: '管理组别',
    prop: 'deptName',
    type: 'select'
  },
  {
    label: '管理分组代码',
    prop: 'deptNo2',
    type: 'input',
    change1: true
  },
  {
    label: '管理分组名称',
    prop: 'deptName2',
    type: 'select'
  },
  {
    label: '提成比率(双)',
    prop: 'rate',
    type: 'input'
  },
  {
    label: '备注',
    prop: 'remark',
    type: 'input'
  }
])

const tableHeader3 = ref([
  {
    label: '工号',
    prop: 'workerId'
  },
  {
    label: '姓名',
    prop: 'name'
  },
  {
    label: '开始日期',
    prop: 'beginDate'
  },
  {
    label: '结束日期',
    prop: 'endDate'
  },
  {
    label: '工种',
    prop: 'workType'
  },
  {
    label: '工资类型',
    prop: 'calcType'
  },
  {
    label: '基本工资',
    prop: 'basicSalary'
  },
  {
    label: '工作天数',
    prop: 'workDays'
  },
  {
    label: '计时工资',
    prop: 'salary'
  }
])

const tableHeader5 = ref([
  {
    label: '部门名称',
    prop: 'deptName'
  }
])
</script>

<style lang="less" scoped>
h1 {
  font-size: 14px;
  margin-bottom: 5px;
}
</style>
